package org.mtmi.ui.scenes;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.mtmi.ui.M;
import org.mtmi.ui.actions.Action;
import org.mtmi.ui.controls.Control;
import org.mtmi.ui.controls.Slider;
import org.mtmi.ui.controls.ToolBar;
import org.mtmi.ui.controls.listeners.ISelectionListener;
import org.mtmi.ui.controls.listeners.SelectionEvent;
import org.mtmi.ui.controls.listeners.ValueChangedListener;
import org.mtmi.ui.controls.listeners.ValueEvent;
import org.mtmi.ui.drawables.IDrawable;
import org.mtmi.ui.drawables.SymbolDrawable;
import org.mtmi.ui.layouts.LayoutData;

public class ZOrderToolBar extends ToolBar
  implements ISceneManagerListener, ISelectionListener, ValueChangedListener
{
	private class CloseAction extends Action
	{
		CloseAction() { super(PUSH); }
		@Override
		public boolean isEnabled() { return true; }
		@Override
		public IDrawable getImage() { 
			return new SymbolDrawable( COLOR.blue, SymbolDrawable.CHECK ) {
				@Override
				public Point computeSize(int width, int height) {
					return  new Point( 32, 32 );
				}
			};
		}
		@Override
		public void run() {
			ZOrderToolBar.this.dispose();
		}
	}

	private static class SimpleSlider extends Slider
	{
		public SimpleSlider( Scene scene, int valCurrent, int valMin, int valMax ) {
			super(scene, true, valCurrent, valMin, valMax, M.SCENE_EDITOR_CONTROL );
			thumbLength = 30;
			paintThumbGrip=false;
		}
		@Override
		protected void paintBackground(GC gc) {}
		@Override
		public Point computeSize(LayoutData l) {
			Point p = super.computeSize(l);
			p.y = Math.max( p.y, 32 );
			return p;
		}
	}

	private Color bg;
	private SimpleSlider slider;
	private List<Control> selection;
	
	public ZOrderToolBar( final Scene scene, int flags ) {
		super( scene, flags|M.SCENE_EDITOR_CONTROL|M.HORIZONTAL );
		
		scene.addSelectionListener( this );
		selection = scene.getSelection();
		//sort it by index:
		Collections.sort( selection, new Comparator<Control>() {
			@Override
			public int compare(Control a, Control b) {
				int i1 = scene.getIndexOf( a );
				int i2 = scene.getIndexOf( b );
				return i1-i2;
			}
		});
		
		SceneManager.getDefault().addSceneManagerListener( this );
		
		//same bounds as GlobalActionToolBar:
		GlobalActionToolBar tb = scene.getGlobalActionToolbar();
		if( tb!=null ) {
			setBounds( tb.getBounds(null) );
		}
		
		int fci = scene.getIndexOf(  selection.get(0) );
		int max = scene.getControlCount( false );
		addControl( slider=new SimpleSlider( scene, fci,0,max), false );
		addAction( new CloseAction(), true );
		
		setForeground( COLOR.yellow );
		bg = CLR.darker( getForeground(), 0.5f );
		setBackground( bg );
		
		slider.setThumbColor( COLOR.light_yellow );
		slider.setForeground( COLOR.yellow );
		slider.addValueChangedListener( this );
	}
	
	@Override
	public void dispose() {
		SceneManager.getDefault().removeSceneManagerListener( this );
		super.dispose();
		bg.dispose();
		slider.dispose();
		scene.setEditMode( Scene.EDIT_CONTROL );
	}

	@Override
	public void sceneManagerModeChanged(boolean edit) {
		if( !edit ) {
			SceneManager.getDefault().removeSceneManagerListener( this );
			dispose();
		}
	}

	@Override
	public void sceneManagerSceneChanged( Scene new_scene, Scene old_scene ) {
		//toolbar only available for selected scene (due to selection):
		dispose();
	}

	@Override
	public void sceneManagerSceneAdded(Scene sceneAdded) {}

	@Override
	public void sceneManagerSceneRemoved(Scene sceneRemoved) {}

	@Override
	public void valueChanged(ValueEvent event) 
	{
		if( !scene.hasSelection() ) return ;
		
		int nv = slider.getValue();
		scene.changeZOrderOf( selection, nv );
	}

	@Override
	public void selectionChanged(SelectionEvent e) {
		selection = scene.getSelection();
		if( scene.hasSelection() ) {
			int nfi = scene.getIndexOf( selection.get(0) );
			slider.removeValueChangedListener( this );
			slider.setValue( nfi );
			slider.addValueChangedListener( this );
		}
	}

	@Override
	public void selectionConfirmed(SelectionEvent e) {}
	
}
